<?php
namespace Shuoke\Controller;
use Think\Controller;
use Weixin\Api\WebpageApi;
class IndexController extends BaseController {
	public function index() {
		$id=I('get.id');
		$data = M ( "topic" )->where("class='".$id."' and ispub=1")->select ();
		
		$this->assign ( "topic", $data );
		
		$this->display ();
	}
	
	
	/**
	 * 热点话题
	 */
	public function hottopic(){
		 $model=new \Think\Model;

        $data = $model->table("wpsk_hottopic h,wpsk_topic t")->where("h.topic_id=t.id")
        				->field('t.*,h.id as hot_id')->select();
        
      	$this->assign("info",$data);
      	
      	$class=M('topicclass')->select();
      	$this->assign("class",$class);
		$this->display ();
	}
	
	/**
	 * 话题详情
	 */
	public function detail(){
		$id=I("get.id");
		$data=M("topic")->where("id='".$id."'")->find();
		
		
			if($data['type']==1){
			$isreplay=M('reply')->where("openid='".$_SESSION['user']['openid']."' and questionnaire_id='".$data['questionnaires_id']."'")->find();
			if($isreplay){
				$data['context']=$data['context']."<br><br><br><br><br><br><br><br><br><div style='text-align:center' ><font color='red'>问卷统计</font></div><br><div style='text-align:center'>
			<a href='http://".$_SERVER['HTTP_HOST']."/shuoke/question/statistic?questionnaireID=".$data['questionnaires_id']."&topicid=".$id."'>查看问卷统计</a></div>
			";
				
//			$data['context']=$data['context']."<br><br><br><div>
//			<iframe style='width:100%;height:1000px'   src='http://".$_SERVER['HTTP_HOST']."/Shuoke/Question/statistic?questionnaireID=".$data['questionnaires_id']."'".'  marginheight="0" marginwidth="0" frameborder="0" scrolling="no"  id="iframepage" name="iframepage" onLoad="iFrameHeight()"'."   >
//			</iframe></div>"
//			.'<script type="text/javascript" language="javascript">
//
//    function iFrameHeight() {
//		
//        var ifm= document.getElementById("iframepage");
//
//        var subWeb = document.frames ? document.frames["iframepage"].document :
//
//ifm.contentDocument;
//
//            if(ifm != null && subWeb != null) {
//
//            ifm.style.height = subWeb.body.scrollHeight+"px";
//
//            }
//
//    }
//
//</script>'
//			;
			}
						else{
			$data['context']=$data['context']."<br><br><br><br><br><br><br><br><br><div style='text-align:center' ><font color='red'>问卷调查</font></div><br><div style='text-align:center'>
			<a href='http://".$_SERVER['HTTP_HOST']."/Weixin/Interview/index.html?questionnaireID=".$data['questionnaires_id']."'>前去填写问卷</a></div>
			";
			}
			}
	
		$commentcount=M("comment")->where("topic_id='".$id."'")->count();
		$comments=M("comment")->where("topic_id='".$id."'")->select();
		$this->assign ( "count", $commentcount );
		$this->assign ( "comment", $comments );
		$this->assign ( "info", $data );
		
		if($data['ispub']==1)
		$this->display ();
		else
		{
		$this->error("此话题没有发布");
		}
	}
	
	/**
	 * 增加评论
	 */
	public function addcomment(){
	$data['topic_id']=$_POST['id'];
	$data['value']=$_POST['content'];
	$data['addtime']=date("Y年m月d日", time());
	$data['author']=$_SESSION['user']['nickname'];
	
	$result=M("comment")->add($data);
	
	 $this->ajaxReturn($data,'JSON');
	
	}
	
 /* 话题添加 */
    public function addtopic()
    {
        if( IS_POST ){ //访问页面
        	$data ["pic"] = $this->upload ( $_FILES ['pic'] );
      		$data['type']=$_POST['type'];
      		$data['title']=$_POST['title'];
      		$data['addtime']=time();
      		$data['author']=$_SESSION['user']['nickname'];
      		$data['class']=$_POST['class'];
      		if($data['type']==0){
      		$data['context']=$_POST['content'];
      		$model = M('topic');
      		$state = $model->add($data);
      		   if( $state===false ){
                    $this->error('话题添加失败，错误信息：'.$model->getDbError());
                }else{
                   $this->success('话题添加成功', U('Index/index'), 0);
                }
      		}
      		else {
      		$data['context']=$_POST['description'];
      		
      		$questionnaires = M('Questionnaires');
      			$qdata ["name"] =$data['title'];
      			$qdata ["iswp"] =0;
				$qdata ["description"] =$data['context']; 
				$qdata ["create_date"] = date("Y-m-d",time()); 
				$qdata ["expire_date"] = date("Y-m-d",time()+ (365 * 24 * 60 * 60));
				$qresult=$questionnaires->add($qdata);
			$data["questionnaires_id"]=$qresult;
			$model = M('topic');
      		$state = $model->add($data);
      		   if( $state===false ){
                    $this->error('问卷添加失败，错误信息：'.$model->getDbError());
                }else{
                   $this->success('问卷添加成功', U('question/index',array('id' =>$qresult,'class'=>$data['class'] )), 0);
                }
      		}
        }
        else{
        		$dataclass=M("topicclass")->select();
        	$this->assign("class",$dataclass);
        
            $this->display();
        }
    }
/**
	 * 上传
	 * @param unknown_type $filename
	 * @return string
	 */
	public function upload($filename) {
		$upload = new \Think\Upload (); // 实例化上传类
		$upload->maxSize = 3145728; // 设置附件上传大小
		$upload->exts = array ('jpg', 'gif', 'png', 'jpeg' ); // 设置附件上传类型
		$upload->rootPath = './Public/Upload/'; // 设置附件上传根目录
		$upload->savePath = '';
		// 上传单个文件 
		$info = $upload->uploadOne ( $filename );
		if (! $info) { // 上传错误提示错误信息
			$this->error ( $upload->getError () );
			exit ();
		} else { // 上传成功 获取上传文件信息
			return $info ['savepath'] . $info ['savename'];
		}
	}
}